<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <link href="${ctx}/css/style.css" type="text/css" rel="stylesheet" /> 
    <link href="${ctx}/js/ztree/css/demo.css" type="text/css" rel="stylesheet">
    <link href="${ctx}/js/ztree/css/zTreeStyle/zTreeStyle.css" type="text/css" rel="stylesheet">
    <script src="${ctx}/js/jquery-1.7.2.min.js" type="text/javascript"></script>
    <script src="${ctx}/js/jquery.cookie.js" type="text/javascript"></script>
    <script src="${ctx}/js/jquery.treeview.js" type="text/javascript"></script>
    <script type="text/javascript" src="${ctx}/js/ztree/js/jquery.ztree.core-3.3.min.js"></script>
    <script type="text/javascript" src="${ctx}/js/ztree/js/jquery.ztree.excheck-3.3.min.js"></script>
  
    <title>安全系统—组织管理-组织授权</title>
<body>
  <div class="m_right">
    
    <div class="m_r_tips">
     <div class="m_r_lefttips">您现在的位置：<a href="${ctx}/admin/home.jsp">首页</a>&gt;组织管理&gt;组织授权</div>
     <jsp:include page="/commons/admin/admin_login_remind.jsp"/>
    </div>
    
    <div class="m_r_list">
    </div>
    
     <s:if test="#session.DO_FEEDBACK != null" >
           <div class="m_r_tips">
               <div class="m_r_lefttips"><span style="color:red">
                  ${DO_FEEDBACK}
                  <% session.removeAttribute("DO_FEEDBACK"); %>
               </span></div>
          </div>
    </s:if>
    
   
    <div class="zTreeDemoBackground left fl" style="width:280px;">
    	<span>&nbsp;</span>
        <ul id="orgTree" class="ztree" style="width:250px;"></ul>
    </div>

    <div class="zTreeDemoBackground left fl" style="width:280px;">
       	<span id="org_name"></span>
        <ul id="permTree" class="ztree" style="width:250px;"></ul>
    </div>
    
    <s:form action="addOrUpdateOrgPerm" method="post" namespace="/admin/role" id="auth_form">
        <input type="hidden" name="origin_perm" value="" id="origin_perm" />
        <input type="hidden" name="change_perm" value="" id="change_perm" />   
        <input type="hidden" name="oid" value="" id="oid" />   
        <div class="zTreeDemoBackground left fl">
            	<span style="display:block;margin-bottom:10px;">&nbsp;</span>
       			<a href="#" class="m_r_bnt1" ><input type="button" value="授权" onclick="auth_permissions();"/></a>
        </div>
    </s:form>
</div>
</body>
  <SCRIPT type="text/javascript">
        <!--
        var org_tree_setting = {
            data: {
                 simpleData: {
                     enable: true
                 }
            },
            callback:{
                    onClick:function(event, treeId, treeNode, clickFlag){ 
                    	fill_perm_tree(treeNode);                    	
                    }
             }
        };

        var perm_tree_setting = {
             check: {
                 enable: true,
                 //被勾选时：关联父，关联子
                 //取消勾选时：关联子
                 chkboxType: { "Y" : "ps", "N" : "s" }
             },  
            data: {
                 simpleData: {
                     enable: true
                 }
            },
            callback:{

            }
        };
       
       var treeData = '${orgTree}';      
       var zNodes = eval('('+treeData+')');

        $(document).ready(function(){
            $.fn.zTree.init($("#orgTree"), org_tree_setting,zNodes);
            var select_node_oid = "${oid}";
            var node;
            if (!select_node_oid || select_node_oid.value == "") {
            	node = zNodes[0];
            } else {
                var treeObj = $.fn.zTree.getZTreeObj("orgTree");
                var nodes = treeObj.getNodesByParam("id", select_node_oid, null);
                node = nodes[0];
            }
            fill_perm_tree(node);   
        });
        
        
        
        function fill_perm_tree(treeNode) {
        	setOriginOid(treeNode);
            $.getJSON("${ctx}/admin/role/getPermTreeOfOrg.do?oid=" + treeNode.id + "&d=" + new Date().getTime(), function(data){
                    setOriginPermission(data);
                    $.fn.zTree.init($("#permTree"), perm_tree_setting,data);
           } );
        }
        
        
        function auth_permissions() {
            var treeObj = $.fn.zTree.getZTreeObj("permTree");
            var nodes = treeObj.getCheckedNodes(true);
            if (nodes.length == 0) {
                if ($("#origin_perm").val() == ""){
                	alert("未选中任何权限");
                	return;
                } else {
                	if (!confirm('确定删除所有权限吗')) {
                		return;
                	}
                }
            }
            
            var change_perm_values= "";
            for (var i=0; i<nodes.length; i++) {
                change_perm_values = change_perm_values + nodes[i].value + "#";
            };
            
            $("#change_perm").val(change_perm_values);
            window.console && console.log("change_perm %o", change_perm_values);
            window.console && console.log("origin_perm %o", $("#origin_perm").val());
            $("#auth_form").submit();
        }
        
        /**
         * 重置和保存组织编码
         */
        function setOriginOid(treeNode) {
        	$("#oid").val("");  //必须记得重置为空值
            $("#oid").val(treeNode.id);
        	$("#org_name").html();
        	$("#org_name").html(treeNode.name);
        }

        /**
         * 重置和保存组织原来的权限
         */
        function setOriginPermission(treeNodes) {
        	$("#origin_perm").val("");  //必须记得重置为空值
            var originPermValues = "";
            for (var i=0; i<treeNodes.length; i++) {
                 if(treeNodes[i].checked) {
                     originPermValues = originPermValues + treeNodes[i].value + "#";
                 }              
            }
            $("#origin_perm").val(originPermValues);
        }
        //-->
    </SCRIPT>
</html>
